Package com.toc.logging

Source Code of com.toc.logging.Log

/*
* @(#) Log.java 1.0 24/12/2009
* Package: com.toc.logging
*
* Copyright 2009 TOC, Studio. All rights reserved.
*/
package com.toc.logging;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.toc.lib.GenericException;
import com.toc.logging.writer.ConsoleWriter;
import com.toc.logging.writer.Writer;
import com.toc.logging.writer.WriterFactory;
import com.toc.logging.config.WriterConfig;

/**
* Title: Logging main program.
* <p>Description:
* <p>This is the main class of the logging component, it contains all useful logging function. To better use the logging function it is recommend to wrap this class use singleton model in your program.
* <p>We provide a common sample of the logging factory, you can start there which help you to easily use the logging component. Please refer to {@link com.toc.logging.LogHelper}
* <p>If you have any idea, bug finding or any suggestion in using the TOC Component please leave your message on <a href="http://sourceforge.net/projects/tocstudio/">TOC Studio on SourceForge</a>.
* <p>Thank you for your selection of using our component.
* <p>Copyright: Copyright (c) 2009 Thousand Origami Cranes Corp.
* <p>Create Time: 25 Dec 2009 19:21:57
* @author Kevin.Zhou
* @version 1.0
* @see com.toc.logging.Configuration
*/
public class Log {
  /*
   * Below is the constant to indicate the output level in logging function.
   * Currently logging function support below 4 level: Debug, Information, Warning and Error.
   */
  /**
   * This is the constant to indicate the information output level.
   * It also been used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   * @see com.toc.logging.Log#log(String, int, int)
   */
  public final static int INFO = 1;
  /**
   * This is the constant to indicate the warning output level.
   * It also been used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   * @see com.toc.logging.Log#log(String, int, int)
   */
  public final static int WARN = 2;
  /**
   * This is the constant to indicate the error output level.
   * It also been used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   * @see com.toc.logging.Log#log(String, int, int)
   */
  public final static int ERROR = 4;
  /**
   * This is the constant to indicate the debug output level.
   * It also been used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   * @see com.toc.logging.Log#log(String, int, int)
   */
  public final static int DEBUG = 8;
 
  /*
   * Below is the constant to indicate the output target in logging function.
   * Currently logging function support below 2 target: Console and File.
   */
  /**
   * This is the constant to indicate the output target is Console.
   * It is used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   */
  public final static int CONSOLE = 1;
  /**
   * This is the constant to indicate the output target is File.
   * It is used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   */
  public final static int FILE = 2;
  /**
   * This is the constant to indicate the output target is Stream.
   * It is used in the Logging Configure Object.
   * Plan to support the stream but the idea of using the stream still under discuss, you can join the discuss via the <a href="https://sourceforge.net/projects/tocstudio/forums/forum/1062752/topic/3500148">Discuss on SourceForg</a>.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   */
  @SuppressWarnings("unused")
  private final static int STREAM = 4;
 
  /*
   * Below is the constant to indicate the frequency to backup the log file in history folder.
   * Currently logging function support below 4 target: Never, Daily, Monthly and Yearly.
   */
  /**
   * This is the constant to indicate the backup function is closed.
   * It is used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   */
  public final static int NEVER = 0;
  /**
   * This is the constant to indicate the backup frequency is Daily.
   * It is used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   */
  public final static int DAILY = 1;
  /**
   * This is the constant to indicate the backup frequency is Monthly.
   * It is used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   */
  public final static int MONTHLY = 2;
  /**
   * This is the constant to indicate the backup frequency is Yearly.
   * It is used in the Logging Configure Object.
   * @see com.toc.logging.Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean) Configuration.addWriter
   */
  public final static int YEARLY = 3;
 
  /*
   * Below is the code been used in TOC Logging Message Pattern.
   * Currently support 6 code as below:
   */
  /**
   * This code indicate the message user input as a parameter via logging method
   * @see Log#log(String, int, int)
   * @see Log#debug(String)
   * @see Log#debug(String, int)
   * @see Log#info(String)
   * @see Log#info(String, int)
   * @see Log#warn(String)
   * @see Log#warn(String, int)
   * @see Log#error(String)
   * @see Log#error(String, int)
   */
  public final static String MESSAGE = "%M";
  /**
   * This code indicate the current system date time, the display format is define in {@link com.toc.logging.config.WriterConfig WriterConfig}.
   * Default format is "yyyy-MM-dd HH:mm:ss".
   */
  public final static String DATE = "%D";
  /**
   * This code indicate the local where the logging function was called.
   * The local information include the package path, class name and method name.
   */
  public final static String CLASS = "%C";
  /**
   * This code indicate the line number where the logging function was called.
   */
  public final static String LINE = "%L";
  /**
   * This code indicate the current thread name.
   */
  public final static String THREAD = "%t";
  /**
   * This code indicate the severity level type of the logging message (DEBUG, INFO, WARN or ERROR).
   * @see Log#log(String, int, int)
   */
  public final static String TYPE = "%T";
 
  /*
   * Below is the default value of the Writer Configuration.
   */
  /**
   * Default Domain, null indicate logging function do not care the domain, writer all message to target.
   */
  public final static String DEFAULT_DOMAIN = null;
  /**
   * Default output severity level type, default is output all message.
   */
  public final static int DEFAULT_OUTPUT = Log.ERROR|Log.WARN|Log.INFO|Log.DEBUG;
  /**
   * Default message pattern. The default pattern is "[%D %T] - %M (%C:%L)".
   */
  public final static String DEFAULT_PATTERN = "[%D %T] - %M (%C:%L)";
  /**
   * Default date pattern. The default pattern is "yyyy-MM-dd HH:mm:ss".
   */
  public final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
  /**
   * Default output target. The default target is Console.
   */
  public final static int DEFAULT_TARGET = Log.CONSOLE;

  /**
   * It is a set of writers which were configured in Configuration Object.
   * Massage will be passed to each writer in this set, the writer will decide whether to print the message.
   * @see com.toc.logging.writer.Writer
   */
  private Collection<Writer> writers = null;
  /**
   * This is the flag to indicate whether the logging function is enabled.
   */
  private boolean enabled = true;
  /**
   * This is the configuration object of the logging function
   *
   */
  private Configuration config = null;
  /**
   * The default writer, if the configuration not define any writer in set, logging function will use this writer to output the message.
   */
  private Writer defaultWriter = null;
 
  /**
   * This method is to output a DEBUG level message
   * <p>Create Time: 29 Dec 2009 18:33:53
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void debug(String message) throws GenericException {
    try {
      debug(message, 1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * This method is to output a DEBUG level message, and this method can pass the depth of the method been call, which can let the logging function get the correct location where logging function been called.
   * If you call the debug method directly, you can pass 1 as the depth. If you wrap the debug method, please increase 1 for each call loop.
   * <p>Create Time: 29 Dec 2009 21:43:37
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @param depth The depth of the call loop. This parameter help you to get correct location of the logging. If you call the debug method directly, you can pass 1 as the depth. If you wrap the debug method, please increase 1 for each call loop.
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void debug(String message, int depth) throws GenericException {
    try {
      log(message, Log.DEBUG, depth+1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * This method is to output a INFO level message
   * <p>Create Time: 31 Dec 2009 19:39:22
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void info(String message) throws GenericException {
    try {
      info(message, 1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * This method is to output a INFO level message, and this method can pass the depth of the method been call, which can let the logging function get the correct location where logging function been called.
   * If you call the debug method directly, you can pass 1 as the depth. If you wrap the debug method, please increase 1 for each call loop.
   * <p>Create Time: 31 Dec 2009 19:40:13
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @param depth The depth of the call loop. This parameter help you to get correct location of the logging. If you call the debug method directly, you can pass 1 as the depth. If you wrap the debug method, please increase 1 for each call loop.
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void info(String message, int depth) throws GenericException {
    try {
      log(message, Log.INFO, depth+1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * This method is to output a WARN level message
   * <p>Create Time: 31 Dec 2009 20:14:43
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void warn(String message) throws GenericException {
    try {
      warn(message, 1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * This method is to output a WARN level message, and this method can pass the depth of the method been call, which can let the logging function get the correct location where logging function been called.
   * <p>Create Time: 31 Dec 2009 21:15:13
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @param depth The depth of the call loop. This parameter help you to get correct location of the logging. If you call the debug method directly, you can pass 1 as the depth. If you wrap the debug method, please increase 1 for each call loop.
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void warn(String message, int depth) throws GenericException {
    try {
      log(message, Log.WARN, depth+1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * This method is to output a ERROR level message
   * <p>Create Time: 31 Dec 2009 21:15:55
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void error(String message) throws GenericException {
    try {
      error(message, 1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * This method is to output a ERROR level message, and this method can pass the depth of the method been call, which can let the logging function get the correct location where logging function been called.
   * <p>Create Time: 31 Dec 2009 21:16:28
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @param depth The depth of the call loop. This parameter help you to get correct location of the logging. If you call the debug method directly, you can pass 1 as the depth. If you wrap the debug method, please increase 1 for each call loop.
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void error(String message, int depth) throws GenericException {
    try {
      log(message, Log.ERROR, depth+1);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * This method is to output a message according to the {@link com.toc.logging.Configuration} object.
   * And it is synchronized to avoid dead lock when writer the message to the file.
   * This method is not visible to the user because of black box principle.
   * <p>Create Time: 31 Dec 2009 21:17:19
   * <br>Author: Kevin.Zhou
   * @param message The message need to be logging
   * @param type The logging message severity level: {@link Log#DEBUG DEBUG},{@link Log#INFO INFO},{@link Log#WARN WARN} and {@link Log#ERROR ERROR}
   * @param depth The depth of the call loop. This parameter help you to get correct location of the logging. If you call the debug method directly, you can pass 1 as the depth. If you wrap the debug method, please increase 1 for each call loop.
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  private synchronized void log(String message, int type, int depth) throws GenericException {
    try {
      //If the logging function is disable, do nothing.
      if(enabled) {
        if(writers.size() > 0) {
          //Fetch each writer in the configuration object and notify the writer to output the message
          for(Iterator<Writer> it = writers.iterator();it.hasNext();) {
            Writer writer = it.next();
            try {
              writer.write(message, type, depth+1);
            } catch(Exception e) {
              //If the the writer raise an error when process the message, remove the writer from the configuration object to avoid the error happen again.
              e.printStackTrace();
              writers.remove(writer);
            }
          }
        } else {
          //If the writer is not been defined in the configuration object, use the default writer to output the message.
          defaultWriter.write(message, type, depth+1);
        }
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * Constrain a new Log object without configuration object.
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public Log() throws GenericException {
    try {
      defaultWriter = new ConsoleWriter(new WriterConfig());
      writers = new ArrayList<Writer>();
      config = new Configuration();
      loadConfig(config);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * Constrain a new Log object with configuration object
   * @param config {@link com.toc.logging.Configuration}
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public Log(Configuration config) throws GenericException {
    try {
      defaultWriter = new ConsoleWriter(new WriterConfig());
      writers = new ArrayList<Writer>();
      loadConfig(config);
   } catch(Exception e) {
     e.printStackTrace();
    }
  }
 
  /**
   * Load a new {@link com.toc.logging.Configuration Configuration} object to configure the logging function.
   * <p>Create Time: Jan 3, 2010 1:44:54 AM
   * <br>Author: Kevin.Zhou
   * @param config {@link com.toc.logging.Configuration Configuration}
   * @throws GenericException Throw the GenericException, refer to com.toc.lib.exception.GenericException.
   */
  public void loadConfig(Configuration config) throws GenericException {
    try {
      this.config = config;
      this.enabled = config.isEnabled();
      writers.clear();
      if(config.getWriters() != null && config.getWriters().size() > 0) {
        for(Iterator<WriterConfig> it = config.getWriters().values().iterator(); it.hasNext();) {
          WriterConfig writerConfig = it.next();
          writers.addAll(WriterFactory.create(writerConfig));
        }
      } else {
        //If no writer is defined in the configuration object, use default writer.
        writers.add(defaultWriter);
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Get the flag of whether the logging function is enabled.
   * <p>Create Time: Jan 3, 2010 2:50:40 PM
   * <br>Author: Kevin.Zhou
   * @return {@link Log#enabled}
   */
  public boolean isEnabled() {
    return enabled;
  }

  /**
   * Set the flag of whether the logging function is enabled.
   * <p>Create Time: Jan 3, 2010 2:51:08 PM
   * <br>Author: Kevin.Zhou
   * @param enabled {@link Log#enabled}
   */
  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
    this.config.setEnabled(enabled);
  }
 
  /**
   * Get logging configuration.
   * <p>Create Time: Jan 10, 2010 12:02:27 AM
   * <br>Author: Kevin.Zhou
   * @return {@link com.toc.logging.Configuration Configuration}
   */
  public Configuration getConfig() {
    return config;
  }
 
  /**
   * Add User-Defined Writer
   * <p>Create Time: May 12, 2011 11:08:47 PM
   * <br>Author: kevin.zhou
   * @param writer
   */
  public void addUserDefinedWriter(Writer writer) {
    this.writers.add(writer);
  }
}
TOP

Related Classes of com.toc.logging.Log

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.